package com.xiyin.demo.db2数据库自定义函数调用;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Main {
    public static void main(String[] args) {
        try {
            Class.forName("com.ibm.db2.jcc.DB2Driver");
            Connection conn = DriverManager.getConnection("jdbc:db2://192.168.70.130:50000/hello:currentSchema=XU_DMP;", "db2inst1", "db2inst1-pwd");

            // 使用 PreparedStatement 来调用 UDF
            String sql = "SELECT XU_DMP.ADDTWONUMBERS(?, ?)";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, 5);
            pstmt.setInt(2, 10);

            ResultSet rs = pstmt.executeQuery();
            if (rs.next()) {
                int result = rs.getInt(1);
                System.out.println("The sum is: " + result);
            }

            rs.close();
            pstmt.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}